$(function() {
	layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function(){
		 var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  ,layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,carousel = layui.carousel //轮播
		  ,upload = layui.upload //上传
		  ,element = layui.element //元素操作
		  ,slider = layui.slider //滑块
	var T= table.render({
	    elem: '#course'
	    ,height: 420
	    ,url: '/selectCourse' //数据接口
	    ,title: '课程表'
	    ,page: true //开启分页
	    ,limit:5
	    ,limits:[5,10,15,20]
	    ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
	    ,totalRow: true //开启合计行
	    ,response : {
            statusCode : 200// 重新规定成功的状态码为 200，table 组件默认为 0
        }
	    ,parseData : function(res) { // res 即为原始返回的数据
            return {
                "count" : res.tol,
                "code" : res.status, // 解析接口状态
                "msg" : res.message, // 解析提示文本
                "data" : res.list
                // 解析数据列表
            };
        }
	    ,cols: [[ //表头
	      {type: 'checkbox', fixed: 'left'}
	     
	      ,{field: 'courseId', title: '课程ID',align:'center', width:180}
	      ,{field: 'courseName', title: '课程名字',align:'center', width: 300}
	      ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo1'}
	    ]]
	  });
		 
		 table.on('tool(course)', function(obj){
			 var data = obj.data;
			 console.log(data);
			var courseId=data.courseId;
			 if(obj.event=='del'){
				 layer.confirm('真的删除行吗',function(index) {
				 $.ajax({
					 url:"/deleteCourse",
             		dataType: 'json',
                     type: 'post',
                     data:{courseId:courseId},
                     success: function () {
                    	 T.reload();
                    	 obj.del();
						layer.close(index);
                     	layer.msg("删除成功");
                     },
                     error: function () {
                        layer.msg("删除失败");
                     }
				 })
				 })
			 }else if(obj.event=='edit'){
				 layer.open({
						type : 1,
						skin : 'layui-layer-rim', // 加上边框
						area : [ '520px', '300px' ], // 宽高
						content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
								+ '  <br>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">课程ID</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <input type="text"  id="courseId" lay-verify="title" autocomplete="off"  readonly disabled class="layui-input layui-disabled">\n'
								+ '    </div>\n'
								+ '  </div>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">课程名字</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <input type="text" id="courseName" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n'
								+ '    </div>\n'
								+ '  </div>\n'
								
								+ '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
								+ '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
								+ '</form>'
					});
				$("#courseId").val(data.courseId);
				$("#courseName").val(data.courseName);
				$("body").on("click","#btn12",function(){
					var courseId=$("#courseId").val();
					var courseName=$("#courseName").val();
					if(courseId!=null && courseName!=null&&courseId!=''&&courseName!=''){
						$.ajax({
							url:'/updateCourse',
							dataType:'json',
							type:'post',
							data:{
								courseId:courseId,
								courseName:courseName	
							},
							success:function(res){
								if(res.message=="修改成功"){
									T.reload();
									layer.closeAll();
									layer.msg("修改成功");
								}else{
									layer.msg("含有相同课程名,不能修改！");
								}
									
						
							},
							error:function(){
									layer.msg("系统错误11");
								
							}
						})
					}else{
						layer.msg("存在空值，请填写完整再提交")
					}
				})
				
				 
			 }
		 })
 
	$("#add").on("click",function(){
		layer.open({
			type : 1,
			skin : 'layui-layer-rim', // 加上边框
			area : [ '520px', '300px' ], // 宽高
			content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
					+ '  <br>\n'
					+ '  <div class="layui-form-item">\n'
					+ '    <label class="layui-form-label">课程ID</label>\n'
					+ '    <div class="layui-input-block">\n'
					+ '      <input type="text" id="courseId" lay-verify="title" autocomplete="off"   class="layui-input">\n'
					+ '    </div>\n'
					+ '  </div>\n'
					+ '  <div class="layui-form-item">\n'
					+ '    <label class="layui-form-label">课程名字</label>\n'
					+ '    <div class="layui-input-block">\n'
					+ '      <input type="text" id="courseName" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n'
					+ '    </div>\n'
					+ '  </div>\n'
					
					+ '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
					+ '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
					+ '</form>'
		});
	$("body").on("click","#btn12",function(){	
		var courseId=$("#courseId").val();
		var courseName=$("#courseName").val();
		if(courseId!=null && courseName!=null&&courseId!=''&&courseName!=''){
			$.ajax({
				url:'/insertCourse',
				dataType:'json',
				type:'post',
				data:{
					courseId:courseId,
					courseName:courseName	
				},
				success:function(res){
					if(res.message=="ok"){
						T.reload();
						layer.closeAll();
						layer.msg("插入成功");
					}else{
						layer.msg("含有相同课程名或相同课程ID！");
					}
						
			
				},
				error:function(){
						layer.msg("系统错误11");
					
				}
			})
		}else{
			layer.msg("存在空值，请填写完整再提交")
		}
		
	})	 
		 
	})	 
		 
	})
})